package com.spdg.ring.sqlite.dao.impl;

import java.util.List;

import android.content.Context;
import cn.wolf.sqlite.dao.impl.BaseDaoImpl;
import cn.wolf.tools.Log;

import com.spdg.ring.sqlite.DBHelper;
import com.spdg.ring.sqlite.dao.ArticleListDao;
import com.spdg.ring.sqlite.model.ArticleList;

/**
 * <p>Title:  ArticleListDaoImpl        </p>
 * <p>Description: 
 * </p>
 * <p>@author: Eric.wsd                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-11-11             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class ArticleListDaoImpl extends BaseDaoImpl<ArticleList> implements ArticleListDao {
	private static ArticleListDaoImpl articleListDao;

	private ArticleListDaoImpl(Context context) {
		super(new DBHelper(context));
	}

	public static ArticleListDaoImpl newInstance(Context context) {
		if (articleListDao == null) {
			articleListDao = new ArticleListDaoImpl(context);
		}

		return articleListDao;
	}

	@Override
	public boolean isExist(ArticleList entity) {
		String sql = "select * from t_article_list where file = ?";

		return articleListDao.isExist(sql, new String[] { entity.getFile() });
	}

	@Override
	public void delete(ArticleList entity) {
		String sql = "delete from t_article_list where file = ?";

		articleListDao.execSql(sql, new String[] { entity.getFile() });
	}

	@Override
	public List<ArticleList> findByParentKey(String fileKey) {
		String sql = "select * from t_article_list where parentfile = ?";

		return articleListDao.rawQuery(sql, new String[] { fileKey });
	}

	@Override
	public ArticleList findByKey(String fileKey) {
		String sql = "select * from t_article_list where file = ?";

		List<ArticleList> list = articleListDao.rawQuery(sql, new String[] { fileKey });
		
		if(list != null && list.size() > 0) {
			return list.get(0);
		}
		
		return null;
	}
	
	@Override
	public void update(ArticleList entity) {
		if(entity == null) {
			Log.e("ArticleList object is null");
			return;
		}
		
		ArticleList article = findByKey(entity.getFile());
		if(article != null) {
			entity.setId(article.getId());
		}
		
		super.update(entity);
	}
}
